<?
require_once $_SERVER['DOCUMENT_ROOT'] . "/kernel/iframe_header.php";
$MAIN->GetHeadStyles();
$MAIN->AddHeadStyle($MAIN->site['template_uri'] . "/less/template.less", "less");
Jscore::Init('less', 'jquery');

if (!$USER->isAdmin()) {
    Logger::error(t('Access denied'));
    Logger::ShowAll();
    die();
}
Module::Init('system.structure');
Module::Init('system.fields');

$bid = $_REQUEST['bid'];
$sid = $_REQUEST['sid'];
$eid = $_REQUEST['eid'];

if (check_action('update_element', 'post')) {
    $props = array();
    $fields = $_POST['field_values'];
    $props['section_id'] = $_POST['section_id'];
    $sid = $_POST['section_id'];
    $props['sort'] = $_POST['sort'];
    BDSElement::Update($_POST['eid'], $props);
    foreach ($fields as $id => $value) {
        FLDValues::Update($id, array('value' => $value));
    }    
}

if (check_action('delete_element', 'post')){
    BDSElement::Delete($_POST['id']);
    $values = FLDValues::GetList(array(
        array('value' => $_POST['id'], 'field' => 'parent_id'),
        array('field' => 'relation', 'value' => 'E')),
        array(),
        array('id'));
    
    $values = $values->GetList();
    foreach($values as $val){
        FLDValues::Delete($val['id']);
    }
    die(t('Element deleted. Close frame'));
}


$element = BDSElement::GetByID($eid);
$fields = Fields::GetFields($eid, $bid, 'E');
if(!$fields) $fields = array();
$sectList = BDSSection::GetList(array(array('field' => 'block_id', 'value' => $bid)));
while ($sect = $sectList->Fetch()) {
    $sect['fields'] = Fields::GetFields($sect['id'], $bid, 'S');
    $sections[] = $sect;
}
$sections = BDSStructure::__tree($sections, 'section_id');

if (!function_exists('GetSectOplionsList')) {
    function GetSectOplionsList($data, $level = 0) {
        global $sid;
        $str = "";
        foreach($data as $item){
            $name = str_repeat("- ", $level).$item['fields']['name']['value'];
            $value = $item['id'];
            $selected = $item['id'] == $sid ? "selected" : "";
            $str .= "<option value=\"$value\" $selected>$name</option>";
            if(!empty($item['children'])){
                $str .= GetSectOplionsList($item['children'], $level+1);
            }
        }
        return $str;
    }
}
?>

<form method="post" enctype="form/multipart-data">
    <div class="separator"><?=t('Properties')?></div>
    <? set_action('update_element') ?>
    <input type='text' name='sort' value='<?= $element['sort'] ?>'/>        
    <select name='section_id'>
        <option value=''>-- --</option>
        <?= GetSectOplionsList($sections); ?>
    </select>
    <div class="separator"><?=t('Fields')?></div>
    <? foreach ($fields as $fld): ?>
        <input type='hidden' name='bid' value='<?= $bid ?>'/>
        <input type='hidden' name='sid' value='<?= $sid ?>'/>
        <input type='hidden' name='eid' value='<?= $eid ?>'/>
        <? if($fld['field_type'] == "text"):?>
            <input type='text' name='field_values[<?= $fld['value_id'] ?>]' value='<?= $fld['value'] ?>'/>        
        <? elseif($fld['field_type'] == "file"): ?>
            <input type='file' name='field_values[<?= $fld['value_id'] ?>]' value='<?= $fld['value'] ?>'/>
        <? endif; ?>
    <? endforeach; ?>    
    <input type='submit'/>
</form>

<form method="post">
    <? set_action('delete_element') ?>
    <input type='hidden' name='id' value='<?= $eid ?>'/>
    <input type='submit' value="Delete"/>
</form>
